var jsclApp = function() {

	// 路径
	var path = Constants.amUrlPath;
	var jscl = {};
	var ractive;
	moment().format();// 时间格式化
	
	var adminZh = Constants.adminZh;
	var adminKtid = Constants.adminKtid;
	function renderView() {
		ractive = new Ractive({
			el : "#jscl-container",
			template : "#main-template",
			data : {
				jscl : jscl
			},
			oncomplete : function() {
				search();
				var o = {};
				getDcl(o, function(data) {
					ractive.set("dcl", data);
				});
			}
		});
		ractive.on({
			plcl:function(){
		    	var ids = $("#wd-table").getAllCheckedId();
				if(!ids){
					alert("Select at least one  settlement record", "info");
					return false;
				}
				//window.open(path+"qzjyjs/pldcJsDcl?ids="+ids);
				openPostWindow(path+"qzjyjs/pldcJsDcl",ids);

			},
			search : function() {
				var jscl_param = ractive.get("jscl");
				jscl_param.zfbddbh = $.trim(jscl_param.zfbddbh);
				jscl_param.jsfzh = $.trim(jscl_param.jsfzh);
				jscl_param.jsfmc = $.trim(jscl_param.jsfmc);
				jscl_param.sqsjks = (jscl_param.sqsjks.length == 10 ? jscl_param.sqsjks + "T00:00:00+08" : jscl_param.sqsjks);
				jscl_param.sqsjjs = (jscl_param.sqsjjs.length == 10 ? jscl_param.sqsjjs + "T23:59:59+08" : jscl_param.sqsjjs);
				ractive.set("jscl.zfbddbh",jscl_param.zfbddbh);
				ractive.set("jscl.jsfzh",jscl_param.jsfzh);
				ractive.set("jscl.jsfmc",jscl_param.jsfmc);
				search(jscl_param);
			}

		})
	}

	//用post请求打开
	function openPostWindow(url,data1){

	    var tempForm = document.createElement("form");
	    tempForm.id = "tempForm1";
	    tempForm.method = "post";
	    tempForm.action = url;
	   // tempForm.target="_blank"; //打开新页面
	    var hideInput1 = document.createElement("input");
	    hideInput1.type = "hidden";
	    hideInput1.name="ids"; //后台要接受这个参数来取值
	    hideInput1.value = data1; //后台实际取到的值
	    tempForm.appendChild(hideInput1);
	    document.body.appendChild(tempForm);
	    tempForm.submit();
	    document.body.removeChild(tempForm);
	}
	
	
	function search(objs) {
		// 默认参数
		var defaults = {
			pageSize : Constants.page.pagesize,
			currPage : 1
		};

		// 合并覆盖查询参数
		var objs = $.extend(defaults, objs);

		// title
		var sqbh = "Alipay order number";
		var jsfzh = "Settlement party account number";
		var jsfmc = "Settlement party name";
		var jsjf = "Settlement  score";
		var jsje = "Settlement amount";
		var sqsj = "Application time";
		var cz = "Operate";
		var _title = [ sqbh, jsfzh, jsfmc, jsjf, jsje, sqsj, cz ];
		initTable(_title, objs);
	}

	function initTable(title, param) {
		var data = [ {
			name : 'zfbddbh',
			width : 100,
			float : 'left',
			innerhtml : function(opts) {
				
				var str = JSON.stringify(opts);
				return "<textarea class='allData' style='display:none'>" + str + "</textarea>" + opts.zfbddbh;
			}
		}, {
			name : 'jsfzh',
			width : 35
		}, {
			name : 'jsfmc',
			width : 35,
			float : 'left'
		}, {
			name : 'jsjf',
			width : 30
		}, {
			name : 'jsje',
			width : 50,
			innerhtml:function(opts){
				if(opts.jsje||opts.jsje=="0"){
					return "¥ " + opts.jsje;
				}
				return "";
				
			}
		}, {
			name : 'sqsj',
			width : 40,
			innerhtml : function(opts) {
				var data = opts.sqsj;
				if (data == '' || data == null) {
					return "";
				} else {
					return moment(data).format("YYYY-MM-DD HH:mm:ss")
				}
			}
		}, {
			name : 'cz',
			width : 30,
			innerhtml : function(opts) {
				var str = JSON.stringify(opts);
				return "<a href='javascript:void(0)' class='ad-gridlist-btn' onclick='jsclApp.submit(" + str + ")' title='Settlement' >Settlement</a>";
			}
		} ];
		// 绘制投诉信息列表
		$("#wd-table").wdGrid({
			url : path + 'qzjyjs/getJsDclpagination',
			colNames : title,
			colModel : data,
			dataType : "GET",
			submitType : "noForm",
			postData : param,
			checkflg : true,
			pager : "#wd-pager",
			checktext:["id"],
			rowList : Constants.page.rowList,
			pagesize : Constants.page.pagesize,
    		headStyle: {
    			background: "#F2F2F2",
				height: 20,
				borderWidth: 1,
				borderColor: '#E1E1E1',
				borderStyle: 'solid',
				borderPosition: ['top', 'left', 'right', 'bottom']
    		}
		});
	}

	function getDcl(o, fn) {

		$.ajax({
			url : path + "qzjyjs/getDclCnt",
			type : "post",
			contentType : 'application/json;charset=UTF-8',
			dateType : "json",
			data : JSON.stringify(o),
			success : function(data) {
				if (data == Constants.NOLOGIN.CODE) {
					return;
				}
				fn.call("", data);
			}
		})
	}

	// 结算
	function submit(obj) {
		var ktid;
		var zh= obj.jsfzh;
		var flag = false;
		
		//合作方关系
		$.ajax({
    		type : "GET",
    		url : path + "qzjyjs/getHzgxbxx?zh="+zh+"&ywy="+obj.ywy,
    		async: false,
    		success : function(data){
    			ktid = data.hzfid;
    		}
    	});

		if(obj.jsflx=="1"){
			alert("Users can not settle!");
			return;
		}
		if(obj.jsflx=="2"){//合作方
			
			window.open("../../ToBatchTrans?jsxx="+obj.id+","+obj.jsje+","+obj.jsfmc+"&ktid="+ktid);
			//结算状态为处理中
			$.ajax({
	    		type : "GET",
	    		url : path + "qzjyjs/updateJsztByID?id="+obj.id,
	    		async: false,
	    		success : function(data){
	    			
	    		}
	    	});
			flag = true;
		}else{
			flag = false;
		}
		
		$(".ad-zxzf-dialogbox, .js_lb_overlay").show();
		$("#qd").click(function(){
			$('#ad-dialog').trigger('close');
			window.location.href = "../../page/qzjygl/qzjygl-jscl.html";
		});
		
	}

	// 批量结算 form提交
	function batchSubmit() {

		$("#pljs").bind("click", function() {

			var num = 0;
			var arr = [];
			$(".allData").each(function() {
				var val = $(this).val();
				var input = $(this).parent().prev().children()[0];
				if (input.checked) {
					num++;
					var obj = eval('(' + val + ')');
					// TODO 结算信息隐藏域信息
					var html = "<input type='hidden' name='js'/>";
					$("#batchJs").append(html);
				}
			});
			if (num > 0) {
				$("#batchJs").submit();
			} else {
				alert("Select at least one  settlement record", "info");
			}

		});
	}

	return {
		init : function() {
			renderView();
		},
		submit : submit
	}

}();
$(document).ready(jsclApp.init);
